package com.app.application;

import android.app.Activity;
import android.app.Application;
import android.app.Service;
import android.content.Context;
import android.os.Handler;
import android.os.Vibrator;

import com.app.utils.NetBroadcastReceiver;
import com.app.xutils.Xutils;
import com.baidu.location.service.LocationService;
import com.baidu.mapapi.SDKInitializer;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
import com.umeng.commonsdk.UMConfigure;
import com.umeng.socialize.PlatformConfig;

import org.xutils.DbManager;
import org.xutils.x;

import java.util.ArrayList;

/**
 * 作者：lblbh on 2017/2/13 14:43
 * 邮箱：lanbuhan@163.com
 * 网名: 前行
 * 功能：全局application  content
 */
public class QXApplication  extends Application  {
    /******************百度基本定位使用服务**************************/
    public LocationService locationService;
    public Vibrator mVibrator;
    /******************百度基本定位使用服务**************************/
    private static QXApplication application;

    //全局网络状态改变
    public static NetBroadcastReceiver.NetEvevt evevt;

    private static Context mContext = null;

    private static Handler mainHandler;

    private static DbManager db;

    ArrayList<Activity> list;


    public QXApplication() {
        list = new ArrayList<Activity>();
    }

    public QXApplication getApplication(){
        return this;
    }

    @Override
    public void onCreate() {
        super.onCreate();

        /***
         * 初始化定位sdk，建议在Application中创建
         */
        SDKInitializer.initialize(getApplicationContext());
        locationService = new LocationService(getApplicationContext());
        mVibrator =(Vibrator)getApplicationContext().getSystemService(Service.VIBRATOR_SERVICE);

        mContext = this;

        mainHandler = new Handler();

        initImageLoader(mContext);

        x.Ext.init(this);

        //选择图片控制器
        Fresco.initialize(getApplicationContext());

        //第三方分享 设置LOG开关，默认为false
        UMConfigure.setLogEnabled(false);
        UMConfigure.init(this,"5a1d3f4e8f4a9d62600000f4"
                ,"Umeng",UMConfigure.DEVICE_TYPE_PHONE,"");
    }



   {//wxa420bccbd00c92c6  b5c6fe8f06c3dde1457e85aa840b8f8e
        PlatformConfig.setWeixin("wx3ba47994b2609b35","e0fea1755a7e2358e1dde61b987cfa18");
        //qq审核暂未通过
//        PlatformConfig.setQQZone("101471476","8762711d05bf7d564c7ffa2041e28e44");
        PlatformConfig.setSinaWeibo("2546686072","dcf900b4ba0456cdffb3ba0e185cef13","http://app.nmgwhy.com/");
    }

    public static void initImageLoader(Context context) {

        ImageLoaderConfiguration.Builder config = new ImageLoaderConfiguration.Builder(context);
        config.threadPriority(Thread.NORM_PRIORITY - 2);
        config.denyCacheImageMultipleSizesInMemory();
        config.diskCacheFileNameGenerator(new Md5FileNameGenerator());
        config.diskCacheSize(50 * 1024 * 1024);
        config.tasksProcessingOrder(QueueProcessingType.LIFO);

        ImageLoader.getInstance().init(config.build());
    }


    public static void createDBUtils(){
        db = x.getDb(Xutils.getDaoConfig());
    }

    public static Context getContext(){
        return mContext;
    }

    public static DbManager getDB(){
        if(db == null){
            createDBUtils();
        }
        return db;
    }

    public static Handler getMainHandler(){
        return mainHandler;
    }

    public static synchronized QXApplication getInstance() {

        if (application == null) {
            application = new QXApplication();
        }
        return application;
    }

    public void addActivity(Activity activity) {
        list.add(activity);
    }

    public void removeActivity() {
        for (int i = 0; i < list.size(); i++) {
            Activity activity = list.get(i);
            activity.finish();
        }
    }




}
